package com.jie.client.service.ui.view;

import com.jie.client.core.ClientApplication;
import com.jie.client.core.handler.ClientWriteHandler;
import com.jie.client.service.entity.model.User;
import com.jie.client.service.ui.constant.ViewMapConstant;
import com.jie.client.service.ui.util.AlertUtil;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.Map;

/**
 * @author XieZhiJie
 * @date 2022/04/27 18:17
 */
public class RegisterView {
    /**
     * 用户帐号信息
     */
    private TextField tUserAccount;

    /**
     * 用户密码信息
     */
    private PasswordField tPassword;

    /**
     * 用户名信息
     */
    private TextField tUsername;

    public Scene getRegisterView() {
        this.tUserAccount = new TextField();
        this.tPassword = new PasswordField();
        this.tUsername = new TextField();

        // 网格布局
        GridPane loginRoot = new GridPane();

        // 显示框(显示"用户名"和"密码")(display)
        Text dUserAccount = new Text("用户账号:");
        Text dPassword = new Text("密码:");
        Text dUsername = new Text("用户名:");

        // 设置输入框大小
        this.tUserAccount.setPrefWidth(200);
        this.tPassword.setPrefWidth(200);
        this.tUsername.setPrefWidth(200);

        // 登录按钮
        Button loginButton = new Button("注册");
        // 给按钮添加事件
        loginButton.setOnAction(event -> onRegisterAction());

        // 返回登录页面
        Button clearButton = new Button("返回");
        // 给按钮添加事件
        clearButton.setOnAction(event -> onBackAction());

        // 设置控件位置
        loginRoot.add(dUserAccount, 0, 0);
        loginRoot.add(tUserAccount, 1, 0);
        loginRoot.add(dPassword, 0, 1);
        loginRoot.add(tPassword, 1, 1);
        loginRoot.add(dUsername, 0, 2);
        loginRoot.add(tUsername, 1, 2);
        loginRoot.add(clearButton, 0,3);
        loginRoot.add(loginButton, 1, 3);

        //设置单独组件(登录按钮)的上下左右的间距
        GridPane.setMargin(loginButton, new Insets(0,0,0,150));

        // 设置其它东西
        loginRoot.setAlignment(Pos.CENTER);
        //设置垂直间距
        loginRoot.setVgap(10);
        //设置水平间距
        loginRoot.setHgap(5);

        // 登录窗口
        return new Scene(loginRoot, 500, 300);
    }

    /**
     * 点击注册
     */
    private void onRegisterAction() {
        // 1.判空处理
        String empty = "";
        if (empty.equals(this.tUserAccount.getText().trim()) || empty.equals(this.tPassword.getText().trim()) || empty.equals(this.tUsername.getText().trim())) {
            AlertUtil.warn("输入信息不能为空!");
            return;
        }
        // 2.封装信息
        User user = new User();
        user.setUserAccount(this.tUserAccount.getText().trim());
        user.setUserPassword(this.tPassword.getText().trim());
        user.setUsername(this.tUsername.getText().trim());
        Map<String, Object> map = new HashMap<>(16);
        map.put("choice", "register");
        map.put("user", user);
        ClientWriteHandler.sendRequest(map);
    }

    /**
     * 点击返回
     */
    public void onBackAction() {
        Stage primaryStage = (Stage) ClientApplication.VIEW_MAP.get(ViewMapConstant.MAIN_STAGE);
        // 返回登录页面
        primaryStage.setTitle("登录");
        primaryStage.setScene(new LoginView().getLoginScene());
        primaryStage.show();
    }

}
